/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.dao;

import br.com.sgs.entity.Funcionario;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Eric
 */

public class DAOFuncionario extends AbstractDAO implements IDAOFuncionario {
    
    public DAOFuncionario(){
        super();
    }
    
    @Override
    public Funcionario findFuncionarioById(int idfuncionario) {
        try {

            Query q = em.createQuery("SELECT f FROM Funcionario f WHERE f.id = :idfuncionario");
            q.setParameter("idfuncionario", idfuncionario);
            
            return (Funcionario) q.getSingleResult();

        } catch (Exception e) {
            System.err.println("Erro no findDepartamentoById");
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Funcionario> findFuncionarioByAll(String nome, String email, String cpf, Date dtcriacaoini, Date dtcriacaofin, List<String> lsstatus) {
        try {

            StringBuilder sql = new StringBuilder();

            if (!nome.equals("")) {
                sql.append("  AND UPPER(f.nome) LIKE '%").append(nome.toUpperCase()).append("%'");
            }
            
            if (!email.equals("")) {
                sql.append("  AND UPPER(f.email) LIKE '%").append(email.toUpperCase()).append("%'");
            }
            
                 
            if (!cpf.equals("")) {
                sql.append("  AND UPPER(f.cpf) LIKE '%").append(email.toUpperCase()).append("%'");
            }
            
            if (dtcriacaoini != null && dtcriacaofin != null) {
                sql.append(" AND f.dtcriacao BETWEEN :dtcriacaoini AND :dtcriacaofin ");
            }

            if (!lsstatus.isEmpty()) {
                String status = "";
                for (String st : lsstatus) {
                    status += st + ",";
                }
                status += lsstatus.get(0).toString();
                sql.append(" AND f.status  IN (").append(status).append(")");
            }

            Query q = em.createQuery("SELECT f FROM Funcionario f WHERE f.id > 0 " + sql.toString() + " ORDER BY f.nome ASC");

            if (dtcriacaoini != null && dtcriacaofin != null) {
                q.setParameter("dtcriacaoini", dtcriacaoini);
                q.setParameter("dtcriacaofin", dtcriacaofin);
            }

            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro na consulta findDepartamentoByAll");
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Funcionario> findFuncionario() {
        try {

            Query q = em.createQuery("SELECT f FROM Funcionario f");
      
            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro no findFuncionario");
            e.printStackTrace();
            return null;
        }
    }
}
